// 名字列表页
var api = require("../../utils/api.js")
Page({
  data: {
    hiddenLoading: true,
    //下拉加锁
    name: '',
    region: '',
    nameList: []
  },
  onLoad: function(options) {
    let self = this;
    this.setData({
      name: options.name,
      act: options.act,
      region: options.region
    })
    this.loadPage();
  },

  //搜索用户昵称
  loadPage: function(cb) {
    var self = this;
    this.setData({
      hiddenLoading: false,
      lock: true
    })
    api.http("h1z1/searchName", "POST", {
      name: encodeURI(self.data.name),
      region: encodeURI(self.data.region)
    }).then((res) => {
      // console.log(res.data)
      let status = res.data.code;
      if (status == 1) {
        self.setData({
          nameList: res.data.datas
        })
      } else {
        wx.showToast({
          title: "数据获取失败",
          duration: 1000
        });
      }
      self.setData({
        lock: false,
        hiddenLoading: true
      })
    }).catch((res) => {
      self.setData({
        lock: false,
        hiddenLoading: true
      })
      wx.showToast({
        title: "数据获取失败",
        duration: 1000
      });
    })
  },

  //绑定操作
  bind: function(value) {
    let self = this;
    let token = wx.getStorageSync('token');
    let region = this.data.region;
    // console.log(token)
    if (value) {
      this.setData({
        hiddenLoading: false,
        btnlock: true
      })
      api.http("h1z1/bind", "POST", {
        token: token,
        region: region,
        gameUserId: value,
      }).then((res) => {
        // console.log(res)
        var status = res.data.code;
        if (status == 1) {
          wx.redirectTo({ url: '../index/index' });
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            showCancel: false
          })
        }
        self.setData({
          hiddenLoading: true
        })
      }).catch((res) => {
        self.setData({
          hiddenLoading: true
        })
        wx.showToast({
          title: "数据获取失败",
          duration: 1000
        });
      })
    }
  },

  //搜索列表点击处理
  tap: function(e) {
    // id代表的是自定义域名，profiles代表你的是steamId
    let self = this;
    let userInfo = this.data.nameList[e.target.dataset.idx];
    let value = userInfo.game_user_id;
    // console.log(value)
    wx.showModal({
      title: '提示',
      content: '是否绑定 ' + userInfo.user_name + '?',
      success: function(res) {
        if (res.confirm) {
          self.bind(value);
        }
      }
    })
  },
  // 分享
  onShareAppMessage: function() {
    return {
      title: 'h1z1群排行',
      path: 'pages/index/index'
    }
  },
  //上拉刷新回调接口
  onReachBottom: function() {
    if (this.data.pageCount >= this.data.page) {
      //在标题栏中显示加载
      　　
      wx.showNavigationBarLoading();
      this.loadPage(function() {
        //完成停止加载
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
      });
    }
  }
})